package com.shf.dynamodb.entity;

import java.util.Set;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIgnore;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBNativeBoolean;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverted;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBVersionAttribute;
import com.shf.dynamodb.converter.DimensionTypeConverter;

@DynamoDBTable(tableName = "ProductCatalog") // 注释将 CatalogItem类映射到ProductCatalog 表
public class CatalogItem {

	private Integer id;
	private String title;
	private String ISBN;
	private Set<String> bookAuthors;
	private String someProp;
	private Boolean inStock = false;
	private Long version;
	private DimensionType dimensionType;

	public CatalogItem() {
	}

	public CatalogItem(Integer id) {
		this.id = id;
	}

	public CatalogItem(Integer id, String title, String ISBN, Set<String> bookAuthors, String someProp) {
		this.id = id;
		this.title = title;
		this.ISBN = ISBN;
		this.bookAuthors = bookAuthors;
		this.someProp = someProp;
	}

	@DynamoDBHashKey(attributeName = "Id")
	// @DynamoDBIndexHashKey(globalSecondaryIndexName = "id-title-Index",
	// attributeName = "Id")
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@DynamoDBAttribute(attributeName = "Title")
	// @DynamoDBIndexRangeKey(globalSecondaryIndexName = "id-title-Index",
	// attributeName = "Title")
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@DynamoDBAttribute(attributeName = "ISBN")
	public String getISBN() {
		return ISBN;
	}

	public void setISBN(String ISBN) {
		this.ISBN = ISBN;
	}

	@DynamoDBAttribute(attributeName = "Authors")
	public Set<String> getBookAuthors() {
		return bookAuthors;
	}

	public void setBookAuthors(Set<String> bookAuthors) {
		this.bookAuthors = bookAuthors;
	}

	@DynamoDBIgnore // 此属性将不被映射至DynamoDB
	public String getSomeProp() {
		return someProp;
	}

	public void setSomeProp(String someProp) {
		this.someProp = someProp;
	}

	@DynamoDBNativeBoolean // 表示类的 boolean（或 Boolean）属性应作为本地 DynamoDB
							// BOOL数据类型进行处理。
	@DynamoDBAttribute(attributeName = "InStock")
	public boolean getInStock() {
		return inStock;
	}

	public void setInStock(boolean inStock) {
		this.inStock = inStock;
	}

	// 乐观锁版本字段
	@DynamoDBVersionAttribute
	public Long getVersion() {
		return version;
	}

	public void setVersion(Long version) {
		this.version = version;
	}

	//自定义任意类型字段
	@DynamoDBTypeConverted(converter = DimensionTypeConverter.class)
	@DynamoDBAttribute(attributeName = "Dimensions")
	public DimensionType getDimensions() {
		return dimensionType;
	}

	public void setDimensions(DimensionType dimensionType) {
		this.dimensionType = dimensionType;
	}

	@Override
	public String toString() {
		return "Book [ISBN=" + ISBN + ", bookAuthors=" + bookAuthors + ", dimensionType= " + dimensionType.getHeight()
				+ " X " + dimensionType.getLength() + " X " + dimensionType.getThickness() + ", Id=" + id + ", Title="
				+ title + "]";
	}

	/**
	 * 自定义验证映射任意数据类型
	 * @author songhaifeng
	 *
	 */
	static public class DimensionType {
		private String length;
		private String height;
		private String thickness;

		public String getLength() {
			return length;
		}

		public void setLength(String length) {
			this.length = length;
		}

		public String getHeight() {
			return height;
		}

		public void setHeight(String height) {
			this.height = height;
		}

		public String getThickness() {
			return thickness;
		}

		public void setThickness(String thickness) {
			this.thickness = thickness;
		}
	}
}
